package com.valuegroup.smarttodo.ui.todo.rethink;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.data.ServiceFactory;
import com.valuegroup.smarttodo.data.model.Rethink;
import com.valuegroup.smarttodo.data.vo.AppCustomCfg;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QucikAddActivity;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QucikAddInputCheckRule;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QuickAddEditorCallback;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QuickAddHolder;
import com.valuegroup.smarttodo.util.AlertUtils;
import com.valuegroup.smarttodo.util.Cfg;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 待办>反思
 */
public class RethinkFragment extends Fragment {
    private static final String TAG = RethinkFragment.class.getSimpleName();
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private Unbinder unbinder;
    private RethinkAdapter adapter;
    private AppCustomCfg cfg;

    public static RethinkFragment newInstance() {
        return new RethinkFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rethink, container, false);
        unbinder = ButterKnife.bind(this, view);
        cfg = Cfg.getCfg(getContext());

        assert recyclerView != null;
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));

        adapter = new RethinkAdapter(view.getContext(), recyclerView);
        recyclerView.setAdapter(adapter);

        setHasOptionsMenu(true);
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        //先clear()一下, 这样按钮就只有Fragment中设置的自己的了, 不会有Activity中的按钮.
        menu.clear();

        inflater.inflate(R.menu.menu_rethink, menu);
    }

    @Override
    public void onPrepareOptionsMenu(@NonNull Menu menu) {
        menu.findItem(R.id.m3).setTitle(!cfg.isRethinkEditModel() ? "编辑模式" : "阅读模式");
        super.onPrepareOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        switch (item.getItemId()) {
            case R.id.m1:
                // 修改内容项
                QucikAddActivity.openEditor(
                        getContext(),
                        new QuickAddEditorCallback() {
                            @Override
                            public void onCancel() {
                            }

                            @Override
                            public void onSubmit(String content) {
                                if (TextUtils.isEmpty(content)) {
                                    return;
                                }

                                // 新增内容
                                Rethink item = new Rethink();
                                item.setPid(0L);
                                item.setTitle(content);
                                adapter.addItem(adapter.getItemCount(), item);

                                //平滑到新增项
                                recyclerView.smoothScrollToPosition(adapter.getItemCount());
                            }

                            @Override
                            public void onAttached(final ViewGroup rootView) {
                            }
                        },
                        new QuickAddHolder(QuickAddHolder.QuickAddType.RETHINK),
                        new QucikAddInputCheckRule(120, 2));
                break;
            case R.id.m2:
                // 批量录入
                QucikAddActivity.openEditorNoRepeat(
                        getContext(),
                        new QuickAddEditorCallback() {
                            @Override
                            public void onCancel() {
                            }

                            @Override
                            public void onSubmit(String content) {
                                if (TextUtils.isEmpty(content)) {
                                    return;
                                }
                                ServiceFactory.getInstance().getRethinkService().multiInsert(content);
                                adapter.refreshListData();
                            }

                            @Override
                            public void onAttached(ViewGroup rootView) {

                            }
                        },
                        new QuickAddHolder(QuickAddHolder.QuickAddType.MULTI_INSERT_FOR_RETHINK),
                        new QucikAddInputCheckRule(2000, 2),
                        Gravity.CENTER);
                break;
            case R.id.m3:
                // 编辑模式
                cfg.setRethinkEditModel(!cfg.isRethinkEditModel());
                Cfg.saveCfg(getContext(), cfg);


                refreshAllData();
                break;
            case R.id.m4:
                // 清空数据
                AlertUtils.delOrderDialog(getContext(),
                        getResources().getString(R.string.alt_clear),
                        (dialog, which) -> {
                            ServiceFactory.getInstance().getRethinkService().getRethinkDao().deleteAll();
                            adapter.refreshListData();
                        });
                break;
        }
        return true;
    }

    /**
     * 更新列表所有数据
     */
    private void refreshAllData() {
        // 刷新列表数据
        adapter.refreshListData();
        // 平移到相关位置
        recyclerView.smoothScrollToPosition(0);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
